﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;

public partial class admin_Gallerys_ListGallery : System.Web.UI.Page
{
    CateGalleryBLL cate = new CateGalleryBLL();
    GalleryBLL gale = new GalleryBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Session["url_back_admin"] = Request.RawUrl;
            LoadCategory();
            ShowData();
        }
    }

    private void LoadCategory()
    {
        ListItem itemRoot = new ListItem("Danh mục gốc", "0");
        dropParent.Items.Insert(0, itemRoot);
        List<CateGallery> listDataSource = cate.GetAll();

        foreach (CateGallery item in listDataSource)
        {
            ListItem obj = new ListItem("--" + item.Name, item.Id.ToString());
            dropParent.Items.Add(obj);
        }
    }


    private void ShowData()
    {
        int parentId = Convert.ToInt32(dropParent.SelectedValue);

        if (dropIsEnable.SelectedIndex == 0)
        {
            if (parentId != 0)
                gridView.DataSource = gale.GetByCategory(parentId);
            else
                gridView.DataSource = gale.GetAll();
        }
        else
        {
            bool isEnable = true;
            if (dropIsEnable.SelectedValue == "2") isEnable = false;

            if (parentId != 0)
                gridView.DataSource = gale.GetByCategory(parentId, isEnable);
            else
                gridView.DataSource = gale.GetAll(isEnable);
        }
        gridView.DataBind();
    }

    private void Enable(bool isEnable)
    {
        foreach (GridViewRow item in gridView.Rows)
        {
            foreach (Control ctrl in item.Cells[1].Controls)
            {
                if (ctrl.GetType() == typeof(CheckBox))
                {
                    CheckBox check = (CheckBox)ctrl;
                    if (check.Checked)
                    {
                        int id = Convert.ToInt32(item.Cells[2].Text);
                        gale.Enable(id, isEnable);
                    }
                    break;
                }
            }
        }
        ShowData();
    }

    protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        try
        {
            if (e.Row.RowIndex >= 0) //Bo qua dong dau
            {
                int id = Convert.ToInt32(e.Row.Cells[2].Text);
                if (e.Row.Cells[4].Text == "0") e.Row.Cells[4].Text = "Danh mục gốc";
                else e.Row.Cells[4].Text = cate.GetById(Convert.ToInt32(e.Row.Cells[4].Text)).Name;

                bool isEnable = Convert.ToBoolean(gale.GetById(id).IsEnable);

                //Icon check
                Image img = new Image();
                foreach (Control item in e.Row.Cells[6].Controls)
                {
                    if (item.GetType() == typeof(Image))
                    {
                        img = (Image)item;
                        break;
                    }
                }
                if (isEnable)
                    img.ImageUrl = "~/Images/check.png";
                else
                {
                    img.ImageUrl = "~/Images/an.png";

                }

            }
        }
        catch { }
    }

    protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        lbThongBao.Text = "";
        try
        {
            gridView.PageIndex = e.NewPageIndex;
            ShowData();
        }
        catch { }
    }
    protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        if ((int)Session["Role"] != 1)
        {
            lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
            return;
        }
        try
        {
            lbThongBao.Text = "";
            int id = Convert.ToInt32(gridView.Rows[e.RowIndex].Cells[2].Text);
            Gallery category = gale.GetById(id);
            //delete image
            Myfunction.DeleteFile("Images/Gallery", category.Image_1);
            Delete_Dequy(id);
            Myfunction.ThongBao(lbThongBao, "Danh mục đã được xóa");

            LoadCategory();
            ShowData();
        }
        catch (Exception ex)
        {
            Myfunction.ThongBao(lbThongBao, ex.Message);
        }
    }
    private void Delete_Dequy(int id)
    {

        List<Gallery> listDataSource = gale.GetByCategory(id);
        foreach (Gallery item in listDataSource)
        {
            if (item.Image_1 != "")
            {
                Myfunction.DeleteFile("Images/Gallery", item.Image_1);
            }
            if (item.Image_2 != "")
            {
                Myfunction.DeleteFile("Images/Gallery", item.Image_2);
            }
            if (item.Image_3 != "")
            {
                Myfunction.DeleteFile("Images/Gallery", item.Image_3);
            }
            if (item.Image_4 != "")
            {
                Myfunction.DeleteFile("Images/Gallery", item.Image_4);
            }
            if (item.Image_5 != "")
            {
                Myfunction.DeleteFile("Images/Gallery", item.Image_5);
            }
            if (item.Image_6 != "")
            {
                Myfunction.DeleteFile("Images/Gallery", item.Image_6);
            }
            Delete_Dequy(item.Id);
        }

        gale.Delete(id);
    }
    protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if ((int)Session["Role"] != 1)
        {
            lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
            return;
        }
        try
        {
            lbThongBao.Text = "";
            ShowData();

        }
        catch (Exception ex)
        {
            Myfunction.ThongBao(lbThongBao, ex.Message);
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/admin/Gallerys/Detail.aspx");
    }
    protected void btnVisible_Click(object sender, EventArgs e)
    {
        if ((int)Session["Role"] != 1)
        {
            lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
            return;
        }
        lbThongBao.Text = "";
        Enable(false);
    }
    protected void btnEnable_Click(object sender, EventArgs e)
    {
        if ((int)Session["Role"] != 1)
        {
            lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
            return;
        }
        lbThongBao.Text = "";
        Enable(true);
    }
    protected void btnXoaChon_Click(object sender, EventArgs e)
    {
        if ((int)Session["Role"] != 1)
        {
            lbThongBao.Text = "Bạn không có quyền thực hiện thao tác này ! ";
            return;
        }
        try
        {
            lbThongBao.Text = "";
            foreach (GridViewRow item in gridView.Rows)
            {
                foreach (Control ctrl in item.Cells[1].Controls)
                {
                    if (ctrl.GetType() == typeof(CheckBox))
                    {
                        CheckBox check = (CheckBox)ctrl;
                        if (check.Checked)
                        {
                            int id = Convert.ToInt32(item.Cells[2].Text);
                            Delete_Dequy(id);
                        }
                        break;
                    }
                }
            }
            LoadCategory();
            ShowData();
        }
        catch (Exception ex)
        {
            Myfunction.ThongBao(lbThongBao, ex.Message);
        }
    }
    protected void linkCheckAll_Click(object sender, EventArgs e)
    {
        lbThongBao.Text = "";
        foreach (GridViewRow item in gridView.Rows)
        {
            foreach (Control ctrl in item.Cells[1].Controls)
            {
                if (ctrl.GetType() == typeof(CheckBox))
                {
                    CheckBox check = (CheckBox)ctrl;
                    check.Checked = true;
                    break;
                }
            }
        }
    }
    protected void linkUnCheckAll_Click(object sender, EventArgs e)
    {
        lbThongBao.Text = "";
        foreach (GridViewRow item in gridView.Rows)
        {
            foreach (Control ctrl in item.Cells[1].Controls)
            {
                if (ctrl.GetType() == typeof(CheckBox))
                {
                    CheckBox check = (CheckBox)ctrl;
                    check.Checked = false;
                    break;
                }
            }
        }
    }
    protected void dropIsEnable_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbThongBao.Text = "";
        ShowData();
    }
    protected void dropCountRow_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbThongBao.Text = "";
        gridView.PageSize = Convert.ToInt32(dropPageSize.SelectedValue);
        ShowData();
    }
    protected void dropParent_SelectedIndexChanged(object sender, EventArgs e)
    {
        lbThongBao.Text = "";
        ShowData();
    }
}